<?

class modTEXT extends MOD {

	function __construct( $vars ) {

		$this->_addvars( $vars );
		$this->_ref();
		
#		Create subclasses
		$this->form = new modTEXT_form( $vars );
	}
	
#	Place some text-specific functionalities here.
}



class modTEXT_form extends MOD {

	function __construct( $vars ) {
		
		$this->_addvars( $vars );
	}
	
#	FILTER.
#	Filter (return multiple) request.

#	Filter form elements, clone attributes
	function filter( $set ) {
	
		if( $set )
			foreach( $set as $id => $attr )

				if( $attr->type == "select" ) {
					
					unset( $attr->type );
					$nodes[] = $this->render_select( $id, (array) $attr );
				
				} else
					$nodes[] = $this->render_input( $id, (array) $attr );
					
		return $nodes;
	}

#	Render value
	function render_value( $value ) {
		global $DB, $Engine;
			
		$what 	= explode('.', $value);

		switch ($what[0]) {
		
		    case 'user':
		    	//echo $what[ 1 ];
		    	if( !empty( $_SESSION[ 'uid' ] ))
			    	$User = new USER((int) $_SESSION[ 'uid' ] );

		    	$return = (( $User ) ? $User->get( $what[1] ) : "");
		    	break;
		    	
		    case 'cal':
		    	$what[0] .= '.' . $what[1];
		    	$what[1]  = $what[2];
		    	unset( $what[2] );
		    	
		    	/*if ($_GET['id']) {
		    		$Obj 	= new CALNODE( $what[0], (int) mysql_real_escape_string($_GET['id']));
		    		$return = $Obj->get($what[1]);
		    		
		    		if ( $what[1] == 'span' ) { $return = $return / 60 / 24; }
		    		else if ( $what[1] == 'date' ) { $return = date('d/m/Y', strtotime( $return )); }
		    			
		    	}*/
		    		
		    	break;
		    	
		    default:
		    	$return = $value;
		}
		
		return $return;	
	}


#	Filter form elements, clone attributes
	function filter_labeled( $set, $rightside = false ) {
		
		if( $set )
			foreach( $set as $id => $attr ) {
						
				$label_attr = (object) array(
					"id"	=> $id,
					"class"	=> $attr->class,
					"value"	=> ( !in_array( $attr->type, array( "button", "submit", "span", "h2", "div" )))? translate( $attr->value ) : "", 
					"dcol"	=> in_array( $attr->type, array( "radio", "checkbox" )) | $attr->dcol,
					"defined" => $attr->defined
				);
								
				if( $attr->type == "select" ) {
				
					$disabled = array();
					if ( $attr->disabled ) $disabled = array( "disabled" => "disabled" );
					
					$node = $this->render_select( $id, array_merge( array( "defined" => $attr->defined ), $disabled ));
				}
				
				else if( $attr->type == "textarea" )
					$node = $this->render_textarea( $attr->value, $label_attr );
					
				else if( in_array( $attr->type, array( "span", "h2", "div" )))
					$node = $this->render_blank( $attr->type, $attr->value, $label_attr );
				
				else {
					unset( $attr->class );
					if( $label_attr->value ) unset( $attr->value );
					if( $label_attr->defined ) 
						$attr->value = $this->render_value( $label_attr->defined );
						unset ( $attr->defined );
					
					$node = $this->render_input( $id, (array) $attr );
				}
				
				$nodes[] = ( $attr->type != "h2" )?
					$this->render_label( $label_attr, $node, $rightside ) : $node;
			}

		return $nodes;	
	}
	
#	Filter all keys (element id's) from form.
	function keys( $children = array() ) {
		
		if( $this->vars->groups )
			foreach( $this->vars->groups as $group ) $children = array_merge( $children, (array) $group );
		
		
		if( $children || $children = $this->vars->children )
			foreach( $children as $key => $val ) if( isset( $_POST[ $key ])) $keys[] = $key;

		
		return $keys; 
	}


#	RENDER.
#	Render (create html) Form-related Elements and content

#	Render form.
	function render( $nodes ) {
		
		if( !$nodes ) $nodes = array();
		return "<form id='" . rand() . "' onSubmit='javascript:return false;'>" . (( $nodes )? implode("\n", $nodes ): "" ) . "</form>\n";
	}
		
#	Render input element.
	function render_input( $id, $attr = array() ) {
		
		$attr = array_merge( array( "id"=> $id, "name"=> $id ), $attr );
		if( $val = $attr[ "value" ])
			$attr[ "value" ] = translate( $attr[ "value" ] );
		
		return "<input ". $this->render_attributes( $attr ) ." />";
	}
	
#	Render normal element.
	function render_blank( $type, $value ) {
		
		return "<$type ". $this->render_attributes( $attr ) .">$value</$type>";
	}

#	Render textarea element.
	function render_textarea( $value, $attr ) {
		
		unset( $attr->defined );
		unset( $attr->dcol );
		return "<textarea name='" . $attr->id . "' ". $this->render_attributes( (array) $attr ) ."></textarea>";
	}

#	Render select element.	
	function render_select( $id, $attr = array(), $children = false ) {
		global $DB;
		
		if ( !empty($this->vars->select->{ $id }[1][0]) && $this->vars->select->{ $id }[1][0] == 'personal' && $this->vars->select->{ $id }[1][1] == 'true' && $this->vars->select->{ $id }[0][1] == 'cal.single' ) {
		
			$journeys = $DB->reference ( 'user', 'parentid = ' . $_SESSION['uid'], $this->vars->select->{ $id }[0][1] );
			
			$options = $DB->filter_objects( $this->vars->select->{ $id }[0][1], "ID in (" . implode(',', $journeys) . ") AND active AND textid != 0" );
			foreach ( $options as $opt ) {
				$cal 		= new CALNODE( $this->vars->select->{ $id }[0][1], (int) $opt->id );
				$value 		= $opt->get('name') . ' (' . $cal->obj->get('name') . ' . ' . $cal->date( 'd/m/Y', ' - d/m/Y' ) . ')';
				$children[] = $this->render_option( array($opt->id, $value), $this->render_value( $attr['defined'] ));
			}
				
		} else if ( !empty($this->vars->select->{ $id }[0][0]) && $this->vars->select->{ $id }[0][0] == 'dynamic' ) {
		
			$options = $DB->filter_objects( $this->vars->select->{ $id }[0][1], "active AND nameid != 0" );
			foreach ( $options as $opt ) $children[] = $this->render_option( array($opt->id, $opt->get('name')), $this->render_value( $attr['defined'] ));
				
		} else {
			
			if ( $children === false && $options = $this->vars->select->{ $id })
				foreach( $options as $opt ) $children[] = $this->render_option( $opt, $this->render_value( $attr['defined'] ) ); 
			
		}
		
		$attr = array_merge( array( "id"=> $id, "name"=> $id ), $attr );
		unset( $attr['defined']); 
		return "<select ". $this->render_attributes( $attr ) .">". (( $children )? implode( "", $children ): "" ) ."</select>";
	}	
	
#	Render option (select) element.	
	function render_option( $input, $defined = false ) {
		
		if( $input[ 1 ]) $attr[ 'value' ] = $input[ 0 ];
		if( $input[ 2 ]) $attr[ $input[ 2 ]] = $input[ 2 ];
		
		echo $this->render_value( $attr->defined );
		
		$selected = "";
		if ( $defined && $defined== $input[0]) $selected = " selected";
		
		return "<option ". $this->render_attributes( $attr ) . $selected . ">". (( $input[1] )? $input[1]: $input[0] ) ."</option>";
	}
	
#	Render label element.	
	function render_label( $input, $child, $rightside = false ) {
		
		$label = "<label for='{$input->id}'>" . translate( $input->value ) . "</label>";
		
		return $rightside?
			"<span class='{$input->class}'>{$child}{$label}</span>" :
			"<span class='{$input->class}'>". ( $input->dcol ? "<br />$label" : "$label<br />" ) ."$child</span>";
	}

}





?>